﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VSWClient.Tcp;
using VSWClient.Utility;
using System.Data;
using VSWClient.Pages.DataGridClass;
using System.Collections.ObjectModel;
using VSWClient.Pages.Tables.L2PDB;

namespace VSWClient.Pages
{
    /// <summary>
    /// Page_Query.xaml interaction logic
    /// </summary>
    public partial class Page_Query : Page
    {
        public Page_Query(TcpClient tcp)
        {
            InitializeComponent();
            client = tcp;
        }
        TcpClient client;
        ObservableCollection<Tasks> tasks = new ObservableCollection<Tasks>();
        ObservableCollection<Strips> taskStrips = new ObservableCollection<Strips>();
        ObservableCollection<Strips> stripStrips = new ObservableCollection<Strips>();
        public int batchStrip_Index = 1;//batchStrip table data current page
        public int batchStrip_Pagenum = 20;//numbers of batcStrip table data per page
        public bool batchStrip_index_isChange = true;
        public int batchPipe_Index = 1;//batchPipe table data current page
        public int batchPipe_Pagenum = 20;//numbers of batchPipe table data per page
        public bool batchPipe_index_isChange = true;

        //string findtaskSql = "";
        //string findstripSql = "";
        //string findpipeSql = "";
        string findrecipeSql = "";

        public int stripStrip_Index = 1;//stripstrip table data current page
        public int stripStrip_Pagenum = 20;//numbers of stripstrip table data per page
        public bool stripStrip_index_isChange = true;
        public int stripPipe_Index = 1;//stripPipe table data current page
        public int stripPipe_Pagenum = 20;//numbers of stripPipe table data per page
        public bool stripPipe_index_isChange = true;
        public string stripMode = "TaskStrip";
        public string taskStripTaskNo = "";

        public int pipePipe_Index = 1;//pipePipe table data current page
        public int pipePipe_Pagenum = 20;//numbers of pipePipe table data per page
        public bool pipePipe_index_isChange = true;
        public string pipeMode = "StripPipe";
        public string stripPipestripNo = "";

        public int bundleBundle_Index = 1;//bundle bundle table data current page
        public int bundleBundle_Pagenum = 20;//numbers of bundle Bundle table data per page
        public bool bundleBundle_index_isChange = true;

        public int recipeLogDetail_Index = 1;//recipeLog table data current page
        public int recipeLogDetail_Pagenum = 20;//numbers of recipelog table data per page
        public bool recipeLogDetail_index_isChange = true;

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //this.dateTimePickerstarttime_Task.Value = Convert.ToDateTime("00:00:00");
            //this.dateTimePickerendtime_Order.Value = Convert.ToDateTime("23:59:59");

            //this.dateTimePickerstarttime_Para.Value = Convert.ToDateTime("00:00:00");
            //this.dateTimePickerendtime_Para.Value = Convert.ToDateTime("23:59:59");	

            this.dateTimePickerstarttime_RecipeLog.Value = Convert.ToDateTime("00:00:00");
        }
        /// <summary>
        /// client table data refresh
        /// </summary>
        /// <param name="dataTable"></param>
        public void RefreshPageQueryList(DataTable dataTable)
        {
            try
            {
                if (dataTable != null)
                {
                    if (dataTable.TableName == TASK_INFO.tableName)
                    {
                        if (dataTable.Rows.Count > 0)
                        {
                            tasks = TabletoDataGridTasks(dataTable);
                            //dataGrid_Task.ItemsSource = tasks;
                            //dataGrid_Task.SelectedIndex = 0;
                        }
                    }
                    else if (dataTable.TableName == STRIP_INFO.tableName)
                    {
                        if (stripMode == "TaskStrip")
                        {
                            Dispatcher.Invoke(new Action(() =>
                            {
                                taskStrips.Clear();
                            }
                                  ));
                            if (dataTable.Rows.Count > 0)
                            {
                                taskStrips = TabletoDataGridStrips(dataTable);
                            }
                            //dataGrid_TaskStrips.ItemsSource = taskStrips;
                        }
                        else if (stripMode == "StripStrip")
                        {
                            Dispatcher.Invoke(new Action(() =>
                            {
                                stripStrips.Clear();
                            }
                                  ));
                            if (dataTable.Rows.Count > 0)
                            {
                                //stripStrips = TabletoDataGridStrips(dataTable);
                                //dataGrid_Strips.ItemsSource = stripStrips;
                                //dataGrid_Strips.SelectedIndex = 0;
                            }
                        }
                    }
                    else if (dataTable.TableName == PIPE_INFO.tableName)
                    {
                        if (pipeMode == "StripPipe")
                        {
                            //dataGrid_StripPipes.ItemsSource = dataTable.DefaultView;							
                        }
                        else if (pipeMode == "PipePipe")
                        {
                            if (dataTable.Rows.Count > 0)
                            {
                                //dataGrid_PipeParameter.ItemsSource = dataTable.DefaultView;
                            }
                        }
                    }
                    else if (dataTable.TableName == RECIPE_EVENT.tableName)
                    {
                        if (dataTable.Rows.Count > 0)
                        {
                            dataGrid_L2RecipeLog.ItemsSource = dataTable.DefaultView;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("RefreshPageQueryList", ex);
                throw ex;
            }

            //else if (dataTable.TableName == STRIP_INFO.tableName)
            //{
            //	dataGrid_Coils.ItemsSource = dataTable.DefaultView;
            //}
            //else if (dataTable.TableName == PIPE_PARAMETER.tableName)
            //{
            //	dataGrid_Parameter.ItemsSource = dataTable.DefaultView;
            //}
            //else if (dataTable.TableName == RECIPE_CHANGELOG.tableName)
            //{
            //	dataGrid_L2RecipeLog.ItemsSource = dataTable.DefaultView;
            //}
        }
        /// <summary>
        /// convert task datatable to datagridclass
        /// </summary>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public ObservableCollection<Tasks> TabletoDataGridTasks(DataTable dataTable)
        {
            try
            {
                ObservableCollection<Tasks> tasksCollection = new ObservableCollection<Tasks>();
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    Tasks dataGridtask = new Tasks();
                    dataGridtask.EventNo = dataTable.Rows[i][TASK_INFO.fd_eventNo].ToString();
                    dataGridtask.TaskNo = dataTable.Rows[i][TASK_INFO.fd_taskNo].ToString();
                    dataGridtask.PipeQuantityBatch = dataTable.Rows[i][TASK_INFO.fd_pipeQuantityBatch].ToString();
                    dataGridtask.ProduceDestination = dataTable.Rows[i][TASK_INFO.fd_produceDestination].ToString();
                    dataGridtask.Priority = dataTable.Rows[i][TASK_INFO.fd_priority].ToString();
                    dataGridtask.BatchSampleRule = dataTable.Rows[i][TASK_INFO.fd_batchSampleRule].ToString();
                    dataGridtask.StripSampleRule = dataTable.Rows[i][TASK_INFO.fd_stripSampleRule].ToString();
                    dataGridtask.RecipeNo = dataTable.Rows[i][TASK_INFO.fd_recipeNo].ToString();
                    dataGridtask.ProductionStandard = dataTable.Rows[i][TASK_INFO.fd_productionStandard].ToString();
                    dataGridtask.PipeDurabilityClass = dataTable.Rows[i][TASK_INFO.fd_pipeDurabilityClass].ToString();
                    dataGridtask.PipeSteelGrade = dataTable.Rows[i][TASK_INFO.fd_pipeSteelGrade].ToString();
                    dataGridtask.PipeNominalMass = dataTable.Rows[i][TASK_INFO.fd_pipeNominalMass].ToString();
                    dataGridtask.PipeNominalLength = dataTable.Rows[i][TASK_INFO.fd_pipeNominalLength].ToString();
                    dataGridtask.PipeMaxLength = dataTable.Rows[i][TASK_INFO.fd_pipeMaxLength].ToString();
                    dataGridtask.PipeMinLength = dataTable.Rows[i][TASK_INFO.fd_pipeMinLength].ToString();
                    dataGridtask.PipeNominalOd = dataTable.Rows[i][TASK_INFO.fd_pipeNominalOd].ToString();
                    dataGridtask.PipeMaxOd = dataTable.Rows[i][TASK_INFO.fd_pipeMaxOd].ToString();
                    dataGridtask.PipeMinOd = dataTable.Rows[i][TASK_INFO.fd_pipeMinOd].ToString();
                    dataGridtask.PipeNominalThickness = dataTable.Rows[i][TASK_INFO.fd_pipeNominalThickness].ToString();
                    dataGridtask.PipeMinThickness = dataTable.Rows[i][TASK_INFO.fd_pipeMinThickness].ToString();
                    dataGridtask.InnerBeadIsremoving = dataTable.Rows[i][TASK_INFO.fd_innerBeadIsremoving].ToString();
                    dataGridtask.InnerBeadHeight = dataTable.Rows[i][TASK_INFO.fd_innerBeadHeight].ToString();
                    dataGridtask.OvalityUpLimit = dataTable.Rows[i][TASK_INFO.fd_ovalityUpLimit].ToString();
                    dataGridtask.BodyStraightness = dataTable.Rows[i][TASK_INFO.fd_bodyStraightness].ToString();
                    dataGridtask.EndStraightness = dataTable.Rows[i][TASK_INFO.fd_endStraightness].ToString();
                    dataGridtask.Status = dataTable.Rows[i][TASK_INFO.fd_status].ToString();
                    dataGridtask.UpdateTime = dataTable.Rows[i][TASK_INFO.fd_updateTime].ToString();

                    Dispatcher.Invoke(new Action(() =>
                    {
                        tasksCollection.Add(dataGridtask);
                    }
                                ));
                }
                LogHelper.WriteLog("TabletoDataGridTasks Success");
                return tasksCollection;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("TabletoDataGridTasks", ex);
                throw ex;
            }

        }
        /// <summary>
        /// convert strip datatable to datagridclass
        /// </summary>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public ObservableCollection<Strips> TabletoDataGridStrips(DataTable dataTable)
        {
            try
            {
                ObservableCollection<Strips> stripsCollection = new ObservableCollection<Strips>();
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    Strips dataGridstrip = new Strips();
                    dataGridstrip.EventNo = dataTable.Rows[i][STRIP_INFO.fd_eventNo].ToString();
                    dataGridstrip.StripNo = dataTable.Rows[i][STRIP_INFO.fd_stripNo].ToString();
                    dataGridstrip.BatchNoA = dataTable.Rows[i][STRIP_INFO.fd_batchNoA].ToString();
                    dataGridstrip.BatchNoB = dataTable.Rows[i][STRIP_INFO.fd_batchNoB].ToString();
                    dataGridstrip.TaskNo = dataTable.Rows[i][STRIP_INFO.fd_taskNo].ToString();
                    dataGridstrip.IsSpy = dataTable.Rows[i][STRIP_INFO.fd_isSpy].ToString();
                    dataGridstrip.IsEx = dataTable.Rows[i][STRIP_INFO.fd_isEx].ToString();
                    dataGridstrip.StripDurabilityClass = dataTable.Rows[i][STRIP_INFO.fd_stripDurabilityClass].ToString();
                    dataGridstrip.StripSteelGrade = dataTable.Rows[i][STRIP_INFO.fd_stripSteelGrade].ToString();
                    dataGridstrip.StripHeat = dataTable.Rows[i][STRIP_INFO.fd_stripHeat].ToString();
                    dataGridstrip.StripThk = dataTable.Rows[i][STRIP_INFO.fd_stripThk].ToString();
                    dataGridstrip.StripWidth = dataTable.Rows[i][STRIP_INFO.fd_stripWidth].ToString();
                    dataGridstrip.StripWeight = dataTable.Rows[i][STRIP_INFO.fd_stripWeight].ToString();
                    dataGridstrip.BurrDirection = dataTable.Rows[i][STRIP_INFO.fd_burrDirection].ToString();
                    dataGridstrip.Status = dataTable.Rows[i][STRIP_INFO.fd_status].ToString();
                    dataGridstrip.UpdateTime = dataTable.Rows[i][STRIP_INFO.fd_updateTime].ToString();

                    Dispatcher.Invoke(new Action(() =>
                    {
                        stripsCollection.Add(dataGridstrip);
                    }
                    ));
                }
                LogHelper.WriteLog("TabletoDataGridStrips");
                return stripsCollection;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("TabletoDataGridStrips", ex);
                MessageBox.Show(ex.Message);
                return null;
            }
        }

        #region RecipeLog Handle
        /// <summary>
        /// query recipe log
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_FindL2RecipeLog_Click(object sender, RoutedEventArgs e)
        {
            string selectStartTime = Convert.ToString(datePicker_StartTime_L2RecipeLog.SelectedDate?.ToString(Formats.GetShortFormat()));
            string selectendTime = Convert.ToString(datePicker_EndTime_L2RecipeLog.SelectedDate?.ToString(Formats.GetShortFormat()));
            string Starttime = Convert.ToString(datePicker_StartTime_L2RecipeLog.SelectedDate?.ToString(Formats.GetShortFormat())) + " "
                          + Convert.ToString(dateTimePickerstarttime_RecipeLog.Value.ToString(Formats.GetTimeFormat()));  //查询开始时间
            string Endtime = Convert.ToString(datePicker_EndTime_L2RecipeLog.SelectedDate?.ToString(Formats.GetShortFormat())) + " "
                           + Convert.ToString(dateTimePickerendtime_RecipeLog.Value.ToString(Formats.GetTimeFormat()));  //查询结束时间
            string strsql = string.Format("select r.*,ROWNUM rn from {0} r where 1=1", RECIPE_EVENT.tableName);
            if (textBox_RecipeId.Text.ToString() != "")
            {
                strsql += string.Format(" and {0} like'{1}'", RECIPE_EVENT.fd_recipeNo, textBox_RecipeId.Text.ToString());
            }
            else
            {
                if (selectStartTime != null && selectendTime == null)
                {
                    strsql += string.Format(" and {0}>='{1}' and {2}<='{3}'", RECIPE_EVENT.fd_updateDate, Starttime, RECIPE_EVENT.fd_updateDate, selectStartTime + " 23:59:59");
                }
                if (selectStartTime == null && selectendTime != null)
                {
                    strsql += string.Format(" and {0}>='{1}' and {2}<='{3}'", RECIPE_EVENT.fd_updateDate, selectendTime + " 00:00:00", RECIPE_EVENT.fd_updateDate, Endtime);
                }
                if (selectStartTime != null && selectendTime != null)
                {
                    strsql += string.Format(" and {0}>='{1}' and {2}<='{3}'", RECIPE_EVENT.fd_updateDate, Starttime, RECIPE_EVENT.fd_updateDate, Endtime);
                }
                if (selectStartTime == null && selectendTime == null)
                {
                    string starttime = DateTime.Now.Date.ToString(Formats.GetDateFormat());
                    string endtime = DateTime.Now.ToString(Formats.GetDateFormat());
                    strsql += string.Format(" and {0}>='{1}' and {2}<='{3}'", RECIPE_CHANGELOG.fd_time, starttime, RECIPE_CHANGELOG.fd_time, endtime);
                }
            }
            findrecipeSql = strsql;
            recipeLogDetail_Index = 1;
            textBox_RecipeLogPage.Text = recipeLogDetail_Index.ToString();
            client.SendMessage(Identity.definedCommand, Identity.queryRecipeLog, findrecipeSql + ";0;20");
        }
        /// <summary>
        /// recipelog up page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_UpPage_RecipeLog_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (recipeLogDetail_Index > 1)
                {
                    recipeLogDetail_Index--;
                    textBox_RecipeLogPage.Text = recipeLogDetail_Index.ToString();
                }
                client.SendMessage(Identity.definedCommand, Identity.queryRecipeLog, findrecipeSql + ";" + ((recipeLogDetail_Index - 1) * recipeLogDetail_Pagenum) + ";" + recipeLogDetail_Index * recipeLogDetail_Pagenum);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("btn_UpPage_RecipeLog_Click", ex);
                throw ex;
            }
        }
        /// <summary>
        /// recipelog down page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_DownPage_RecipeLog_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (Convert.ToInt32(textBox_RecipeLogPage.Text) < Convert.ToInt32(textBox_RecipeLogTotalPages.Text))
                {
                    recipeLogDetail_Index++;
                    textBox_RecipeLogPage.Text = recipeLogDetail_Index.ToString();
                    client.SendMessage(Identity.definedCommand, Identity.queryRecipeLog, findrecipeSql + ";" + ((recipeLogDetail_Index - 1) * recipeLogDetail_Pagenum) + ";" + recipeLogDetail_Index * recipeLogDetail_Pagenum);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("btn_DownPage_RecipeLog_Click", ex);
                throw ex;
            }
        }
        /// <summary>
        /// different states display different colors
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGrid_L2RecipeLog_LoadingRow(object sender, DataGridRowEventArgs e)
        {

        }
        #endregion

        #region Batch Handle
        /// <summary>
        /// query task,strip,pipe information according to batchno
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_FindBatch_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string batchNo = textBox_BatchNo.Text.ToString();
                client.SendMessage(Identity.definedCommand, Identity.queryBatchByBatchNo, batchNo);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("btn_FindBatch_Click", ex);
                throw ex;
            }
        }
        /// <summary>
        /// refresh client batch table data
        /// </summary>
        /// <param name="tables"></param>
        /// <param name="stripTotalPages"></param>
        /// <param name="pipeToatalPages"></param>
        /// <param name="dic"></param>
        public void RefreshPageQueryBatch(DataTable[] tables, string stripTotalPages, string pipeToatalPages, Dictionary<string, double> dic)
        {
            try
            {
                if (tables.Length == 3)
                {
                    DataTable taskTable = tables[0];
                    if (taskTable.Rows.Count == 1)
                    {
                        textBox_QueryBatchTaskNo.Text = taskTable.Rows[0][TASK_INFO.fd_taskNo].ToString();
                        textBox_QueryBatchPipeQuaBatch.Text = taskTable.Rows[0][TASK_INFO.fd_pipeQuantityBatch].ToString();
                        textBox_QueryBatchProDestination.Text = taskTable.Rows[0][TASK_INFO.fd_produceDestination].ToString();
                        textBox_QueryBatchPriority.Text = taskTable.Rows[0][TASK_INFO.fd_priority].ToString();
                        textBox_QueryBatchProStandard.Text = taskTable.Rows[0][TASK_INFO.fd_productionStandard].ToString();
                        textBox_QueryBatchPipeDurClass.Text = taskTable.Rows[0][TASK_INFO.fd_pipeDurabilityClass].ToString();
                        textBox_QueryBatchPipeSteelGrade.Text = taskTable.Rows[0][TASK_INFO.fd_pipeSteelGrade].ToString();
                        textBox_QueryBatchPipeNomMass.Text = taskTable.Rows[0][TASK_INFO.fd_pipeNominalMass].ToString();
                        textBox_QueryBatchPipeNomLength.Text = taskTable.Rows[0][TASK_INFO.fd_pipeNominalLength].ToString();
                        textBox_QueryBatchPipeNomOd.Text = taskTable.Rows[0][TASK_INFO.fd_pipeNominalOd].ToString();
                        textBox_QueryBatchPipeNomThickness.Text = taskTable.Rows[0][TASK_INFO.fd_pipeNominalThickness].ToString();
                        textBox_QueryBatchStatus.Text = taskTable.Rows[0][TASK_INFO.fd_status].ToString();
                    }

                    DataTable stripTable = tables[1];
                    if (stripTable.Rows.Count > 0)
                    {
                        DataTable newStripTable = new DataTable();
                        newStripTable = stripTable.DefaultView.ToTable(STRIP_INFO.tableName, true, new string[] { STRIP_INFO.fd_stripNo, STRIP_INFO.fd_stripHeat, STRIP_INFO.fd_status, STRIP_INFO.fd_updateTime });
                        dataGrid_QueryBatchStrip.ItemsSource = newStripTable.DefaultView;
                        textBox_QueryBatchStripCurrentPage.Text = "1";
                        textBox_QueryBatchStripTotalPages.Text = stripTotalPages;
                    }

                    DataTable pipeTable = tables[2];
                    if (pipeTable.Rows.Count > 0)
                    {
                        DataTable newPipeTable = new DataTable();
                        newPipeTable = pipeTable.DefaultView.ToTable(PIPE_INFO.tableName, true, new string[] { PIPE_INFO.fd_pipeNo, PIPE_INFO.fd_length, PIPE_INFO.fd_qualityCode, PIPE_INFO.fd_location, PIPE_INFO.fd_updateTime });
                        dataGrid_QueryBatchPipe.ItemsSource = newPipeTable.DefaultView;
                        textBox_QueryBatchPipeCurrentPage.Text = "1";
                        textBox_QueryBatchPipeTotalPages.Text = pipeToatalPages;

                        if (dic.Count == 9)
                        {
                            textBox_QueryBatchPipeTotalNumber.Text = dic["pipeTotalNums"].ToString();
                            textBox_QueryBatchPipeTotalLength.Text = dic["pipeTotalLength"].ToString();
                            textBox_QueryBatchPipeTotalWeight.Text = dic["pipeTotalWeight"].ToString();
                            textBox_QueryBatchPipeGoodNumber.Text = dic["pipeGoodTotalNums"].ToString();
                            textBox_QueryBatchPipeGoodlength.Text = dic["pipeGoodTotalLength"].ToString();
                            textBox_QueryBatchPipeGoodWeight.Text = dic["pipeGoodTotalWeight"].ToString();
                            textBox_QueryBatchPipeBadNumber.Text = dic["pipeScrapTotalNums"].ToString();
                            textBox_QueryBatchPipeBadlength.Text = dic["pipeScrapTotalLength"].ToString();
                            textBox_QueryBatchPipeBadWeight.Text = dic["pipeScrapTotalWeight"].ToString();
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("RefreshPageQueryBatch", ex);
                throw ex;
            }
        }
        /// <summary>
        /// batch strip page up
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_QueryBatchStrip_PageUp_Click(object sender, RoutedEventArgs e)
        {
            if (batchStrip_Index > 1)
            {
                batchStrip_Index--;
                textBox_QueryBatchStripCurrentPage.Text = batchStrip_Index.ToString();
                client.SendMessage(Identity.definedCommand, Identity.queryBatchStripByBatchNo, textBox_BatchNo.Text.ToString() + ";" + ((batchStrip_Index - 1) * batchStrip_Pagenum) + ";" + batchStrip_Index * batchStrip_Pagenum);
            }
        }
        /// <summary>
        /// batch strip page down
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_QueryBatchStrip_PageDown_Click(object sender, RoutedEventArgs e)
        {
            if (Convert.ToInt32(textBox_QueryBatchStripCurrentPage.Text) < Convert.ToInt32(textBox_QueryBatchStripTotalPages.Text))
            {
                batchStrip_Index++;
                textBox_QueryBatchStripCurrentPage.Text = batchStrip_Index.ToString();
                client.SendMessage(Identity.definedCommand, Identity.queryBatchStripByBatchNo, textBox_BatchNo.Text.ToString() + ";" + ((batchStrip_Index - 1) * batchStrip_Pagenum) + ";" + batchStrip_Index * batchStrip_Pagenum);
            }
        }
        /// <summary>
        /// batch pipe page up
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_QueryBatchPipe_PageUp_Click(object sender, RoutedEventArgs e)
        {
            if (batchPipe_Index > 1)
            {
                batchPipe_Index--;
                textBox_QueryBatchPipeCurrentPage.Text = batchPipe_Index.ToString();
                client.SendMessage(Identity.definedCommand, Identity.queryBatchPipeByBatchNo, textBox_BatchNo.Text.ToString() + ";" + ((batchPipe_Index - 1) * batchPipe_Pagenum) + ";" + batchPipe_Index * batchPipe_Pagenum);
            }
        }
        /// <summary>
        /// batch pipe page down
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_QueryBatchPipe_PageDown_Click(object sender, RoutedEventArgs e)
        {
            if (Convert.ToInt32(textBox_QueryBatchPipeCurrentPage.Text) < Convert.ToInt32(textBox_QueryBatchPipeTotalPages.Text))
            {
                batchPipe_Index++;
                textBox_QueryBatchPipeCurrentPage.Text = batchPipe_Index.ToString();
                client.SendMessage(Identity.definedCommand, Identity.queryBatchPipeByBatchNo, textBox_BatchNo.Text.ToString() + ";" + ((batchPipe_Index - 1) * batchPipe_Pagenum) + ";" + batchPipe_Index * batchPipe_Pagenum);
            }
        }
        /// <summary>
        /// pagination display strip,pipe table information in batch
        /// </summary>
        /// <param name="dataTable"></param>
        public void RefreshPageQueryBatchList(DataTable dataTable)
        {
            try
            {
                if (dataTable != null)
                {
                    if (dataTable.TableName == STRIP_INFO.tableName)
                    {
                        if (dataTable.Rows.Count > 0)
                        {
                            DataTable newStripTable = new DataTable();
                            newStripTable = dataTable.DefaultView.ToTable(STRIP_INFO.tableName, true, new string[] { STRIP_INFO.fd_stripNo, STRIP_INFO.fd_stripHeat, STRIP_INFO.fd_status, STRIP_INFO.fd_updateTime });
                            dataGrid_QueryBatchStrip.ItemsSource = newStripTable.DefaultView;
                        }
                    }
                    else if (dataTable.TableName == PIPE_INFO.tableName)
                    {
                        if (dataTable.Rows.Count > 0)
                        {
                            DataTable newPipeTable = new DataTable();
                            newPipeTable = dataTable.DefaultView.ToTable(PIPE_INFO.tableName, true, new string[] { PIPE_INFO.fd_pipeNo, PIPE_INFO.fd_length, PIPE_INFO.fd_qualityCode, PIPE_INFO.fd_location, PIPE_INFO.fd_updateTime });
                            dataGrid_QueryBatchPipe.ItemsSource = newPipeTable.DefaultView;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("RefreshPageQueryBatchList", ex);
                throw ex;
            }
        }
        #endregion

        #region Strip Handle	
        /// <summary>
        /// according to stripno fuzzy query strip information
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_FindStrip_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string stripNo = textBox_StripNo.Text.ToString();
                textBox_QueryStripStripCurrentPage.Text = "1";
                client.SendMessage(Identity.definedCommand, Identity.queryStripStripByStripNo, stripNo + ";" + "0;20");

            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("btn_FindStrip_Click", ex);
                throw ex;
            }
        }
        /// <summary>
        /// Strip Strip table data page up
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_QueryStripStrip_PageUp_Click(object sender, RoutedEventArgs e)
        {
            if (stripStrip_Index > 1)
            {
                string stripNo = textBox_StripNo.Text.ToString();
                stripStrip_Index--;
                textBox_QueryStripStripCurrentPage.Text = stripStrip_Index.ToString();
                client.SendMessage(Identity.definedCommand, Identity.queryStripStripByStripNo, stripNo + ";" + ((stripStrip_Index - 1) * stripStrip_Pagenum) + ";" + stripStrip_Index * stripStrip_Pagenum);
            }
        }
        /// <summary>
        /// Strip Strip table data page down
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_QueryStripStrip_PageDown_Click(object sender, RoutedEventArgs e)
        {
            if (Convert.ToInt32(textBox_QueryStripStripCurrentPage.Text) < Convert.ToInt32(textBox_QueryStripStripTotalPages.Text))
            {
                string stripNo = textBox_StripNo.Text.ToString();
                stripStrip_Index++;
                textBox_QueryStripStripCurrentPage.Text = stripStrip_Index.ToString();
                client.SendMessage(Identity.definedCommand, Identity.queryStripStripByStripNo, stripNo + ";" + ((stripStrip_Index - 1) * stripStrip_Pagenum) + ";" + stripStrip_Index * stripStrip_Pagenum);
            }
        }
        /// <summary>
        ///  Strip Strip table data SelectionChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGrid_QueryStrip_Strip_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var drv = dataGrid_QueryStrip_Strip.SelectedItem as DataRowView;
                textBox_QueryStrip_StripNo.Text = drv[STRIP_INFO.fd_stripNo].ToString();
                textBox_QueryStrip_BatchNoE.Text = drv[STRIP_INFO.fd_batchNoA].ToString();
                textBox_QueryStrip_BatchNoO.Text = drv[STRIP_INFO.fd_batchNoB].ToString();
                textBox_QueryStrip_TaskNo.Text = drv[STRIP_INFO.fd_taskNo].ToString();
                textBox_QueryStrip_IsSPY.Text = drv[STRIP_INFO.fd_isSpy].ToString();
                textBox_QueryStrip_IsEX.Text = drv[STRIP_INFO.fd_isEx].ToString();
                textBox_QueryStrip_StripDurabilityClass.Text = drv[STRIP_INFO.fd_stripDurabilityClass].ToString();
                textBox_QueryStrip_StripSteelGrade.Text = drv[STRIP_INFO.fd_stripSteelGrade].ToString();
                textBox_QueryStrip_StripHeat.Text = drv[STRIP_INFO.fd_stripHeat].ToString();
                textBox_QueryStrip_StripTHK.Text = drv[STRIP_INFO.fd_stripThk].ToString();
                textBox_QueryStrip_StripWidth.Text = drv[STRIP_INFO.fd_stripWidth].ToString();
                textBox_QueryStrip_StripWeight.Text = drv[STRIP_INFO.fd_stripWeight].ToString();
                textBox_QueryStrip_BurrDirection.Text = drv[STRIP_INFO.fd_burrDirection].ToString();
                textBox_QueryStrip_Status.Text = drv[STRIP_INFO.fd_status].ToString();
                textBox_QueryStrip_UpdateTime.Text = drv[STRIP_INFO.fd_updateTime].ToString();

                textBox_StripPipeCurrentPage.Text = "1";
                client.SendMessage(Identity.definedCommand, Identity.queryStripPipeByStripNo, textBox_QueryStrip_StripNo.Text.ToString() + ";0;20");
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("dataGrid_QueryStrip_Strip_SelectionChanged", ex);
                throw ex;
            }
        }
        /// <summary>
        /// Strip Pipe table data page up
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_QueryStripPipe_PageUp_Click(object sender, RoutedEventArgs e)
        {
            if (stripPipe_Index > 1)
            {
                stripPipe_Index--;
                textBox_StripPipeCurrentPage.Text = stripPipe_Index.ToString();
                client.SendMessage(Identity.definedCommand, Identity.queryStripPipeByStripNo, textBox_QueryStrip_StripNo.Text.ToString() + ";" + ((stripPipe_Index - 1) * stripPipe_Pagenum) + ";" + stripPipe_Index * stripPipe_Pagenum);
            }
        }
        /// <summary>
        /// Strip Pipe table data page down
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_QueryStripPipe_PageDown_Click(object sender, RoutedEventArgs e)
        {
            if (Convert.ToInt32(textBox_StripPipeCurrentPage.Text) < Convert.ToInt32(textBox_StripPipeTotalPages.Text))
            {
                stripPipe_Index++;
                textBox_StripPipeCurrentPage.Text = stripPipe_Index.ToString();
                client.SendMessage(Identity.definedCommand, Identity.queryStripPipeByStripNo, textBox_QueryStrip_StripNo.Text.ToString() + ";" + ((stripPipe_Index - 1) * stripPipe_Pagenum) + ";" + stripPipe_Index * stripPipe_Pagenum);
            }
        }
        /// <summary>
        /// pagination display strip pipe table information in strip
        /// </summary>
        /// <param name="dataTable"></param>
        public void RefreshPageQueryStripList(DataTable dataTable)
        {
            try
            {
                if (dataTable != null)
                {
                    if (dataTable.TableName == STRIP_INFO.tableName)
                    {
                        if (dataTable.Rows.Count > 0)
                        {
                            //DataTable newStripTable = new DataTable();
                            //newStripTable = dataTable.DefaultView.ToTable(STRIP_INFO.tableName, true, new string[] { STRIP_INFO.fd_stripNo, STRIP_INFO.fd_stripHeat, STRIP_INFO.fd_status, STRIP_INFO.fd_updateTime });
                            dataGrid_QueryStrip_Strip.ItemsSource = dataTable.DefaultView;
                        }
                    }
                    else if (dataTable.TableName == PIPE_INFO.tableName)
                    {
                        if (dataTable.Rows.Count > 0)
                        {
                            DataTable newPipeTable = new DataTable();
                            newPipeTable = dataTable.DefaultView.ToTable(PIPE_INFO.tableName, true, new string[] { "rn", PIPE_INFO.fd_pipeNo, PIPE_INFO.fd_length, PIPE_INFO.fd_qualityCode, PIPE_INFO.fd_location, PIPE_INFO.fd_updateTime });
                            dataGrid_QueryStrip_Pipe.ItemsSource = newPipeTable.DefaultView;
                        }
                        else
                        {
                            dataGrid_QueryStrip_Pipe.ItemsSource = dataTable.DefaultView;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("RefreshPageQueryBatchList", ex);
                throw ex;
            }
        }
        #endregion

        #region Pipe Handle	
        /// <summary>
        /// according to pipeno fuzzy query Pipe information
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_FindPipeParameter_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string pipeNo = textBox_QueryPipe_PipeNo.Text.ToString();
                textBox_QueryPipeCurrentPage.Text = "1";
                client.SendMessage(Identity.definedCommand, Identity.queryPipePipeByPipeNo, pipeNo + ";" + "0;20");
                LogHelper.WriteLog("btn_FindPipeParameter_Click");
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("btn_FindPipeParameter_Click", ex);
                throw ex;
            }
        }
        /// <summary>
        /// Pipe Pipe table data page up
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_QueryPipe_PageUp_Click(object sender, RoutedEventArgs e)
        {
            if (pipePipe_Index > 1)
            {
                pipePipe_Index--;
                textBox_QueryPipeCurrentPage.Text = pipePipe_Index.ToString();
                client.SendMessage(Identity.definedCommand, Identity.queryPipePipeByPipeNo, textBox_QueryPipe_PipeNo.Text.ToString() + ";" + ((pipePipe_Index - 1) * pipePipe_Pagenum) + ";" + pipePipe_Index * pipePipe_Pagenum);
            }
        }
        /// <summary>
        /// Pipe Pipe table data page down
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_QueryPipe_PageDown_Click(object sender, RoutedEventArgs e)
        {
            if (Convert.ToInt32(textBox_QueryPipeCurrentPage.Text) < Convert.ToInt32(textBox_QueryPipeTotalPages.Text))
            {
                pipePipe_Index++;
                textBox_QueryPipeCurrentPage.Text = pipePipe_Index.ToString();
                client.SendMessage(Identity.definedCommand, Identity.queryPipePipeByPipeNo, textBox_QueryPipe_PipeNo.Text.ToString() + ";" + ((pipePipe_Index - 1) * pipePipe_Pagenum) + ";" + pipePipe_Index * pipePipe_Pagenum);
            }
        }
        /// <summary>
        /// Pipe Pipe table data SelectionChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGrid_QueryPipe_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var drv = dataGrid_QueryPipe.SelectedItem as DataRowView;
                if (drv != null)
                {
                    string pipeNo = drv[PIPE_INFO.fd_pipeNo].ToString();
                    textBox_QueryPipe_InputTaskNo.Text = drv[PIPE_INFO.fd_taskNo].ToString();
                    textBox_QueryPipe_InputBatchNo.Text = drv[PIPE_INFO.fd_batchNo].ToString();
                    client.SendMessage(Identity.definedCommand, Identity.queryPipePipePassportAndTaskByPipeNo, pipeNo);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("dataGrid_QueryPipe_SelectionChanged", ex);
            }
        }
        /// <summary>
        /// pagination display pipe information
        /// </summary>
        /// <param name="dataTable"></param>
        public void RefreshPageQueryPipeList(DataTable dataTable)
        {
            try
            {
                if (dataTable != null)
                {
                    if (dataTable.TableName == PIPE_INFO.tableName)
                    {
                        if (dataTable.Rows.Count > 0)
                        {
                            //DataTable newPipeTable = new DataTable();
                            //newPipeTable = dataTable.DefaultView.ToTable(PIPE_INFO.tableName, true, new string[] { "rn", PIPE_INFO.fd_pipeNo, PIPE_INFO.fd_length, PIPE_INFO.fd_qualityCode, PIPE_INFO.fd_location, PIPE_INFO.fd_updateTime });
                            dataGrid_QueryPipe.ItemsSource = dataTable.DefaultView;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("RefreshPageQueryBatchList", ex);
                throw ex;
            }
        }
        /// <summary>
        ///after selecting pipe data,refresh Pipepassport and task information
        /// </summary>
        /// <param name="dataTable"></param>
        /// <param name="dic"></param>
        public void RefreshPageQueryPipePassportList(DataTable dataTable, Dictionary<string, string> dic)
        {
            try
            {
                textBox_QueryPipe_PipeID.Text = dic[PIPE_INFO.fd_pipeNo];
                textBox_QueryPipe_Date.Text = dic["DATE"];
                textBox_QueryPipe_Time.Text = dic["TIME"];
                textBox_QueryPipe_PipeODWT.Text = dic["PIPEODWT"];
                textBox_QueryPipe_ProductionStandard.Text = dic[TASK_INFO.fd_productionStandard];
                textBox_QueryPipe_PipeGroupStrength.Text = dic[TASK_INFO.fd_pipeDurabilityClass];
                textBox_QueryPipe_PipeSteelGrade.Text = dic[TASK_INFO.fd_pipeSteelGrade];
                textBox_QueryPipe_StripNo.Text = dic["SEQUENNUMINSTRIP"];
                textBox_QueryPipe_PipeQuantity.Text = dic["SEQUENNUMINSTRIP"];
                textBox_QueryPipe_SignBatch.Text = dic[STRIP_INFO.fd_isEx];
                textBox_QueryPipe_SignTest.Text = dic[STRIP_INFO.fd_isSpy];
                textBox_QueryPipe_ShiftID.Text = dic["SHIFTID"];

                textBox_QueryPipeTaskNo.Text = dataTable.Rows[0][TASK_INFO.fd_taskNo].ToString();
                textBox_QueryPipeProDestination.Text = dataTable.Rows[0][TASK_INFO.fd_produceDestination].ToString();
                textBox_QueryPipeProStandard.Text = dataTable.Rows[0][TASK_INFO.fd_productionStandard].ToString();
                textBox_QueryPipePipeDurClass.Text = dataTable.Rows[0][TASK_INFO.fd_pipeDurabilityClass].ToString();
                textBox_QueryPipePipeSteelGrade.Text = dataTable.Rows[0][TASK_INFO.fd_pipeSteelGrade].ToString();
                textBox_QueryPipePipeNomMass.Text = dataTable.Rows[0][TASK_INFO.fd_pipeNominalMass].ToString();
                textBox_QueryPipePipeNomLength.Text = dataTable.Rows[0][TASK_INFO.fd_pipeNominalLength].ToString();
                textBox_QueryPipePipeMaxLength.Text = dataTable.Rows[0][TASK_INFO.fd_pipeMaxLength].ToString();
                textBox_QueryPipePipeMinLength.Text = dataTable.Rows[0][TASK_INFO.fd_pipeMinLength].ToString();
                textBox_QueryPipePipeNomOd.Text = dataTable.Rows[0][TASK_INFO.fd_pipeNominalOd].ToString();
                textBox_QueryPipePipeMaxOd.Text = dataTable.Rows[0][TASK_INFO.fd_pipeMaxOd].ToString();
                textBox_QueryPipePipeMinOd.Text = dataTable.Rows[0][TASK_INFO.fd_pipeMinOd].ToString();
                textBox_QueryPipePipeNomThickness.Text = dataTable.Rows[0][TASK_INFO.fd_pipeNominalThickness].ToString();
                textBox_QueryPipePipeMinThickness.Text = dataTable.Rows[0][TASK_INFO.fd_pipeMinThickness].ToString();
                textBox_QueryPipeInnerBeadIsremoving.Text = dataTable.Rows[0][TASK_INFO.fd_innerBeadIsremoving].ToString();
                textBox_QueryPipeInnerBeadHeight.Text = dataTable.Rows[0][TASK_INFO.fd_innerBeadHeight].ToString();
                textBox_QueryPipeInnerBeadDepth.Text = dataTable.Rows[0][TASK_INFO.fd_innerBeadDepth].ToString();
                textBox_QueryPipeOvalityUpLimit.Text = dataTable.Rows[0][TASK_INFO.fd_ovalityUpLimit].ToString();
                textBox_QueryPipeBodyStraightness.Text = dataTable.Rows[0][TASK_INFO.fd_bodyStraightness].ToString();
                textBox_QueryPipeEndStraightness.Text = dataTable.Rows[0][TASK_INFO.fd_endStraightness].ToString();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("RefreshPageQueryPipePassportList", ex);
                throw ex;
            }
        }
        /// <summary>
        /// export pipeProcessdata data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_QueryPipe_Export_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                client.SendMessage(Identity.definedCommand, Identity.queryPipeExportPipeProcessData, textBox_QueryPipe_PipeID.Text);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("btn_QueryPipe_Export_Click", ex);
            }
        }
        /// <summary>
        /// print Pipe information
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_QueryPipe_Print_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (comboBox_QueryPipe_PrinterName.SelectedItem == null)
                {
                    MessageBox.Show("please select a printer first");
                    return;
                }
                string printerName = ((System.Windows.Controls.ComboBoxItem)comboBox_QueryPipe_PrinterName.SelectedItem).Content.ToString();
                DataTable pipedataTable = new DataTable();
                pipedataTable.Columns.Add("Pipe ID", typeof(string));
                pipedataTable.Columns.Add("date", typeof(string));
                pipedataTable.Columns.Add("time", typeof(string));
                pipedataTable.Columns.Add("Pipe", typeof(string));
                pipedataTable.Columns.Add("Production standard", typeof(string));
                pipedataTable.Columns.Add("Group(class)strength", typeof(string));
                pipedataTable.Columns.Add("Steel grade", typeof(string));
                pipedataTable.Columns.Add("No of strip", typeof(string));
                pipedataTable.Columns.Add("Odinal No of strip", typeof(string));
                pipedataTable.Columns.Add("sign of the batch", typeof(string));
                pipedataTable.Columns.Add("sign of the test", typeof(string));
                pipedataTable.Columns.Add("Shift ID", typeof(string));
                pipedataTable.TableName = "DataTablePipeName";
                pipedataTable.Rows.Add(textBox_QueryPipe_PipeID.Text, textBox_QueryPipe_Date.Text, textBox_QueryPipe_Time.Text, textBox_QueryPipe_PipeODWT.Text, textBox_QueryPipe_ProductionStandard.Text, textBox_QueryPipe_PipeGroupStrength.Text,
                    textBox_QueryPipe_PipeSteelGrade.Text, textBox_QueryPipe_StripNo.Text, textBox_QueryPipe_PipeQuantity.Text, textBox_QueryPipe_SignBatch.Text, textBox_QueryPipe_SignTest.Text, textBox_QueryPipe_ShiftID.Text);
                Printer printer = new Printer(pipedataTable);
                printer.Print(true, printerName, 2);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("btn_QueryPipe_Print_Click", ex);
            }
        }
        #endregion

        #region Bundle Handle
        /// <summary>
        /// uncheck row inforamtion
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGrid_QueryBundle_MouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                dataGrid_QueryBundle.SelectedItem = null;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("dataGrid_QueryBundle_MouseDown", ex);
            }
        }
        /// <summary>
        /// according to bundleno fuzzy query bundle and pipe information
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_QueryFindBundle_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string bundleNo = textBox_QueryBundle_BundleNo.Text.ToString();
                textBox_QueryBundleCurrentPage.Text = "1";
                client.SendMessage(Identity.definedCommand, Identity.queryBundleByBundleNo, bundleNo + ";" + "0;20");
                LogHelper.WriteLog("btn_QueryFindBundle_Click");
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("btn_QueryFindBundle_Click", ex);
                throw ex;
            }
        }
        /// <summary>
        /// pagination display bundle table information
        /// </summary>
        /// <param name="dataTable"></param>
        public void RefreshPageQueryBundleList(DataTable dataTable)
        {
            try
            {
                if (dataTable != null)
                {
                    if (dataTable.TableName == BUNDLE_TABLE.tableName)
                    {
                        if (dataTable.Rows.Count > 0)
                        {
                            //DataTable newPipeTable = new DataTable();
                            //newPipeTable = dataTable.DefaultView.ToTable(PIPE_INFO.tableName, true, new string[] { "rn", PIPE_INFO.fd_pipeNo, PIPE_INFO.fd_length, PIPE_INFO.fd_qualityCode, PIPE_INFO.fd_location, PIPE_INFO.fd_updateTime });
                            dataGrid_QueryBundle.ItemsSource = dataTable.DefaultView;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("RefreshPageQueryBundleList", ex);
                throw ex;
            }
        }
        /// <summary>
        /// bundle bundle table data page up
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_QueryBundle_PageUp_Click(object sender, RoutedEventArgs e)
        {
            if (bundleBundle_Index > 1)
            {
                bundleBundle_Index--;
                textBox_QueryBundleCurrentPage.Text = bundleBundle_Index.ToString();
                client.SendMessage(Identity.definedCommand, Identity.queryBundleByBundleNo, textBox_QueryBundle_BundleNo.Text.ToString() + ";" + ((bundleBundle_Index - 1) * bundleBundle_Pagenum) + ";" + bundleBundle_Index * bundleBundle_Pagenum);
            }

        }
        /// <summary>
        /// bundle bundle table data page down
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_QueryBundle_PageDown_Click(object sender, RoutedEventArgs e)
        {
            if (Convert.ToInt32(textBox_QueryBundleCurrentPage.Text) < Convert.ToInt32(textBox_QueryBundleTotalPages.Text))
            {
                bundleBundle_Index++;
                textBox_QueryBundleCurrentPage.Text = bundleBundle_Index.ToString();
                client.SendMessage(Identity.definedCommand, Identity.queryBundleByBundleNo, textBox_QueryBundle_BundleNo.Text.ToString() + ";" + ((bundleBundle_Index - 1) * bundleBundle_Pagenum) + ";" + bundleBundle_Index * bundleBundle_Pagenum);
            }
        }
        /// <summary>
        /// select bundle row data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGrid_QueryBundle_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var drv = dataGrid_QueryBundle.SelectedItem as DataRowView;
                if (drv != null)
                {
                    StackPanel_BatchPipe.Children.Clear();
                    string bundleID = drv[BUNDLE_TABLE.fd_bundleNo].ToString();
                    client.SendMessage(Identity.definedCommand, Identity.queryBundlePipeByBundleNo, bundleID);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("dataGrid_QueryBundle_SelectionChanged", ex);
                throw ex;
            }
        }
        /// <summary>
        /// display pipe information in bundle
        /// </summary>
        /// <param name="bundlePipe"></param>
        /// <param name="batchPipe"></param>
        public void RefreshPageQueryBundlePipeList(Dictionary<string, string> bundlePipe, Dictionary<string, DataTable> batchPipe, DataTable bundleTotal)
        {
            try
            {
                textBox_QueryBundle_bundleID.Text = bundlePipe[BUNDLE_TABLE.fd_bundleNo];
                textBox_QueryBundle_Date.Text = bundlePipe["DATE"];
                textBox_QueryBundle_Time.Text = bundlePipe["TIME"];
                textBox_QueryBundle_PipeODPipeTHK.Text = bundlePipe["PIPEODWT"];
                textBox_QueryBundle_ProductionStandard.Text = bundlePipe[TASK_INFO.fd_productionStandard];
                textBox_QueryBundle_PipeGroupStrength.Text = bundlePipe[TASK_INFO.fd_pipeDurabilityClass];
                textBox_QueryBundle_PipeSteelGrade.Text = bundlePipe[TASK_INFO.fd_pipeSteelGrade];
                textBox_QueryBundle_batchNums.Text = bundlePipe["BATCH_NUMBERS"];
                textBox_QueryBundle_PipeQuantity.Text = bundlePipe["PIPE_QUANLITY"];
                textBox_QueryBundle_totalLength.Text = bundlePipe["TOTAL_LENGTH"];
                textBox_QueryBundle_bundleWeight.Text = bundlePipe["BUNDLE_WEIGHT"];
                textBox_QueryBundle_ShiftID.Text = bundlePipe["SHIFT_ID"];
                textBox_QueryBundle_Cradle.Text = bundlePipe[BUNDLE_TABLE.fd_cradle];

                foreach (KeyValuePair<string, DataTable> kv in batchPipe)
                {
                    UserControl_BatchPipe userControl_BatchPipe = new UserControl_BatchPipe();
                    userControl_BatchPipe.textBox_BatchNo.Text = kv.Key;
                    userControl_BatchPipe.dataGrid_BatchPipe.ItemsSource = batchPipe[kv.Key].DefaultView;
                    StackPanel_BatchPipe.Children.Add(userControl_BatchPipe);
                }
                UserControl_BundleTotal userControl_BundleTotal = new UserControl_BundleTotal();
                userControl_BundleTotal.dataGrid_BundleTotal.ItemsSource = bundleTotal.DefaultView;
                StackPanel_BatchPipe.Children.Add(userControl_BundleTotal);
                dicBundlePipe = batchPipe;
                dicBundleTotal = bundleTotal;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("RefreshPageQueryBundlePipeList", ex);
            }
        }
        /// <summary>
        /// print bundle and pipe information
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        Dictionary<string, DataTable> dicBundlePipe = new Dictionary<string, DataTable>();
        DataTable dicBundleTotal = new DataTable();
        private void btn_QueryBundle_Print_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (comboBox_QueryBundle_PrinterName.SelectedItem == null)
                {
                    MessageBox.Show("Please select a printer first");
                    return;
                }
                string printerName = ((System.Windows.Controls.ComboBoxItem)comboBox_QueryBundle_PrinterName.SelectedItem).Content.ToString();
                DataTable bundledataTable = new DataTable();
                bundledataTable.Columns.Add("Bundle ID", typeof(string));
                bundledataTable.Columns.Add("date", typeof(string));
                bundledataTable.Columns.Add("time", typeof(string));
                bundledataTable.Columns.Add("Pipe", typeof(string));
                bundledataTable.Columns.Add("standard", typeof(string));
                bundledataTable.Columns.Add("strength class:", typeof(string));
                bundledataTable.Columns.Add("Steel grade", typeof(string));
                bundledataTable.Columns.Add("Batch numbers", typeof(string));
                bundledataTable.Columns.Add("pipe Qty in bundle", typeof(string));
                bundledataTable.Columns.Add("Total length, m", typeof(string));
                bundledataTable.Columns.Add("bundle weight, kg", typeof(string));
                bundledataTable.Columns.Add("Shift ID", typeof(string));
                bundledataTable.Columns.Add("cradle", typeof(string));
                bundledataTable.TableName = "DataTablebundleName";
                bundledataTable.Rows.Add(textBox_QueryBundle_bundleID.Text, textBox_QueryBundle_Date.Text, textBox_QueryBundle_Time.Text, textBox_QueryBundle_PipeODPipeTHK.Text, textBox_QueryBundle_ProductionStandard.Text, textBox_QueryBundle_PipeGroupStrength.Text,
                    textBox_QueryBundle_PipeSteelGrade.Text, textBox_QueryBundle_batchNums.Text, textBox_QueryBundle_PipeQuantity.Text, textBox_QueryBundle_totalLength.Text, textBox_QueryBundle_bundleWeight.Text, textBox_QueryBundle_ShiftID.Text, textBox_QueryBundle_Cradle.Text);
                string batchNums = textBox_QueryBundle_batchNums.Text;
                //List<string> listBatch =new List<string>(batchNums.Split(','));
                //Printer printerbundle = new Printer(bundledataTable,listBatch);
                //printerbundle.Print(true, printerName, 1);

                bundledataTable.TableName = "DataTableBundlePipeName";
                Printer printerBundlePipe = new Printer(bundledataTable, dicBundlePipe, dicBundleTotal);
                printerBundlePipe.Print(true, printerName, 2);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("btn_QueryBundle_Print_Click", ex);
            }
        }

        #endregion

        #region RecipeLog Handle		
        #endregion


    }
}
